home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / inet / ien / ien-74 < prev    next >
Text File  |  1988-12-01  |  13KB  |  440 lines

  1.  
  2. IEN-74
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.                         Sequence Number Arithmetic
  14.  
  15.  
  16.  
  17.                             William W. Plummer
  18.  
  19.  
  20.                        Bolt Beranek and Newman, Inc.
  21.                              50 Moulton Street
  22.                            Cambridge MA   02138
  23.  
  24.  
  25.                              21 September 1978
  26.  
  27.  
  28.  
  29.  
  30. Internet Experiment Note 74                              21 September 1978
  31. Sequence Number Arithmetic                               William W. Plummer
  32.  
  33.  
  34. 1.      Introduction
  35.  
  36. TCP deals with 32-bit numbers for sequencing  and  acknowledging  data.   A
  37. basic  concept  is  that  of  a "window", a range of sequence numbers which
  38. begins at a "left" pointer and ends at a "right" pointer.  Because a window
  39. may contain sequence number zero, deciding whether a given number is within
  40. a window is somewhat complicated.  This paper describes some techniques for
  41. dealing with sequence numbers as they apply to TCP.
  42.  
  43.  
  44. 2.      Representation
  45.  
  46. The space of 2**32 sequence numbers will be shown as a (rough) circle:
  47.  
  48.  
  49.                           0
  50.                           | 2**32 - 1
  51.                         .....
  52.                        .     .
  53.         Increasing |  .       .
  54.         Numbers    |  .       .
  55.                    V  .       .
  56.                        .     .
  57.                         .....
  58.  
  59. Segments of sequence numbers will be shown as:
  60.  
  61.                 ........************.........>
  62.                                            Increasing numbers
  63.  
  64.                         ^           ^
  65.                         |           |
  66.                       Left         Right
  67.  
  68. Note that Left is the the first number within the segment.   Right  is  not
  69. included.  That is, the segment is semi-open.  If Left = Right, the segment
  70. is considered to have zero length, not 2**32.
  71.  
  72.  
  73.  
  74. 3.      The CheckWindow Function
  75.  
  76.  
  77. Because the sequence number space is actually a ring and arithmetic is done
  78. modulo  2**32, there is no concept of one sequence number being greater (or
  79. less) than  another.   The  fundamental  function  for  comparing  sequence
  80. numbers  is CheckWindow(Left, Sequence, Right).  This function returns true
  81. if Sequence is contained in the semi-open segment between Left  and  Right.
  82.  
  83.  
  84.                                    - 1 -
  85.  
  86.  
  87.  
  88.  
  89. Internet Experiment Note 74                              21 September 1978
  90. Sequence Number Arithmetic                               William W. Plummer
  91.  
  92.  
  93. For  machines  with  word  sizes  greater  than  32-bits  or using unsigned
  94. arithmetic on 32-bit machines, the definition of CheckWindow is:
  95.  
  96.         CheckWindow(L, S, R) := (L le R) =>
  97.                                         L le S and S lt R  ,
  98.                                   not ( R le S and S lt L)
  99.  
  100. The second branch of the conditional is expressed in a way that it  is  the
  101. complement of the first branch when  L  and  R are interchanged.  Advantage
  102. is  taken of this symmetry in the PDP-10 code which implements CheckWindow.
  103. Otherwise the second branch may be expressed as  (R gt S) or (S ge L).
  104.  
  105. The first branch of the conditional is explained by the following  diagram:
  106.  
  107.                            0
  108.                            | 2**32 - 1
  109.                            |
  110.                            |oooo
  111.                          xxx    o
  112.                         x  |     o
  113.                        x .....    o
  114.                       x .     .    o
  115.                      x .       .    o
  116.                      x .       .    o <--- Right
  117.         Left --> o   x *       * x  o
  118.                  o   x  *     *  x  o
  119.                   o   x  *****  x  o
  120.                    o   x       x  o
  121.                     o   xxxxxxx  o
  122.                      o          o
  123.                       oooooooooo
  124.  
  125.         Key:    ......  Basic sequence space
  126.  
  127.                 ******  Segment between Left and Right
  128.  
  129.                 xxxxxx  Sequence space where Sequence lt Right
  130.  
  131.                 oooooo  Sequence space where Sequence ge Left
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.                                    - 2 -
  144.  
  145.  
  146.  
  147.  
  148. Internet Experiment Note 74                              21 September 1978
  149. Sequence Number Arithmetic                               William W. Plummer
  150.  
  151.  
  152. The  second branch of the conditional is the case where the segment crosses
  153. zero:
  154.                           0
  155.                           | 2**32 - 1
  156.                           |
  157.                           |oooo
  158.                        xxxx    o
  159.                       x   |     o
  160.                      x  *****    o
  161.                     x  *     *    o
  162.                     x *       *   o
  163.                     x *       *   o
  164.         Right -->     .       *   o  <-- Left
  165.                        .     .
  166.                         .....
  167.  
  168.  
  169.         Key:    .....   Sequence space
  170.  
  171.                 *****   Segment between Left and Right
  172.  
  173.                 ooooo   Sequence numbers ge Left
  174.  
  175.                 xxxxx   Sequence numbers lt Right
  176.  
  177.  
  178. A useful identity is:  CheckWindow(L, S, R) =  not  CheckWindow(R,  S,  L).
  179. This  says  that  either   S  is in the segment between  L  and  R or it is
  180. not.
  181.  
  182.  
  183.  
  184.  
  185. 4.      OverLap(L1, R1, L2, R2)
  186.  
  187. OverLap(L1, R1, L2, R2)  is a predicate which tells if the two segments  L1
  188. to  R1   and   L2  to R2  have at least one point in common.  If an overlap
  189. exists, then one segment must have its Left end within the other:
  190.  
  191.         OverLap(L1, R1, L2, R2) :=
  192.  
  193.          CheckWindow(L1, L2, R1) or CheckWindow(L2, L1, R2)
  194.  
  195. Either  L2  is within segment one or it is not.  So either  CheckWindow(L1,
  196. L2, R1)  or  not CheckWindow(L2, L1, R2)  is true.  In the first case there
  197. is  an overlap even if it is just at the point  L2.  The second term can be
  198. rewritten:
  199.  
  200.  
  201.  
  202.                                    - 3 -
  203.  
  204.  
  205.  
  206.  
  207. Internet Experiment Note 74                              21 September 1978
  208. Sequence Number Arithmetic                               William W. Plummer
  209.  
  210.  
  211.         not CheckWindow(L2, L1, R2)  =  CheckWindow(R2, L1, L2).
  212.  
  213. Since L2 is outside segment one, it is the position of R2 which  determines
  214. whether an overlap exists.  R2 can be either between L2  and  L1  or it can
  215. be   between    L1   and   L2.   Thus,  there  are  two  subcases:   either
  216. CheckWindow(L2, R2, L1)  or  CheckWindow(L1, R2, L2) must be true.  In  the
  217. first  case  there  is no overlap and segment one does not contain  R2.  If
  218. the first case is not true then the second case must be  since  it  is  the
  219. complement of the first with the first and third arguments switched.
  220.  
  221.  
  222.  
  223. 5.      Include(L1, R1, L2, R2)
  224.  
  225.  
  226. Include  is  true if segment one includes all of segment two.  This is true
  227. only if the complement of segment one does not contain any of segment  two.
  228.  
  229.         Include(L1, R1, L2, R2) := not Overlap(R1, L1, L2, R2)
  230.  
  231.                 = CheckWindow(L1, L2, R1) and CheckWindow(R2, R1, L2)
  232.  
  233. The  expansion  states  that   L2   must  lie  in  segment one and that the
  234. complement of segment two must contain the right end of segment one.
  235.  
  236.  
  237.  
  238. 6.      Uses Within a TCP
  239.  
  240. The functions CheckWindow, Overlap, and Include have many uses  within  the
  241. TCP.   A few of these are described below.  Some definitions are needed.  A
  242. TCB contains a Send.Left cell, a Send.Window, and Send.Sequence  having  to
  243. do with packet generation.  Send.Right does not exist explicitly but may be
  244. computed by adding Send.Left and Send.Window (mod 2**32).
  245.  
  246. The  receive side of a connection has the cells Recv.Left and Recv.Window .
  247. Again, Recv.Right may be easily computed.
  248.  
  249. Each packet has its sequence number Pkt.Seq and an  acknowledgement  number
  250. Pkt.AckSeq.   The number called Pkt.End may be computed by counting one for
  251. each control bit and data byte in the packet and adding this to Pkt.Seq mod
  252. 2**32.  Note that Pkt.End is actually the  sequence  number  following  the
  253. packet.   Currently  only SYN and FIN occupy sequence space and these occur
  254. only at the start and end of a connection; otherwise, all sequence space is
  255. occupied by data only.
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                    - 4 -
  262.  
  263.  
  264.  
  265.  
  266. Internet Experiment Note 74                              21 September 1978
  267. Sequence Number Arithmetic                               William W. Plummer
  268.  
  269.  
  270. These variables define several segments.  The send window between Send.Left
  271. and Send.Right,  the receive window between Recv.Left and  Recv.Right,  and
  272. the  packet segment between Pkt.Seq and Pkt.End.  All of these segments are
  273. semi-open and are suitable for manipulation  by  the  previously  described
  274. functions such as CheckWindow.
  275.  
  276. The  Retransmitter uses OverLap(Send.Left, Send.Right, Pkt.Seq, Pkt.End) to
  277. tell if a packet has anything transmittable in it.   Note  that  Send.Right
  278. may  lie within the segment between Send.Left and Send.Seq.  This indicates
  279. that the window shrank due to Send.Right having moved "backwards".  In this
  280. case  data  between  Send.Right   and   Send.Seq   is   (temporarily)   not
  281. retransmittable.
  282.  
  283. The  InputProcessor  makes  heavy  use  of all of the functions.  The basic
  284. acceptance test for  packets  arriving  on  an  ESTABLISHED  connection  is
  285. OverLap(Recv.Left, Recv.Right, Pkt.Seq, Pkt.End).  If this is not true, the
  286. packet  is  assumed  to  be  either from the future or a duplicate from the
  287. past.
  288.  
  289. Processing the Acknowledgement field of a packet involves  a  scan  of  the
  290. retransmission  queue to see which packets may be deleted.  For each packet
  291. on the queue CheckWindow(Send.Left, Pkt.End-1, Acknowledgement) is true  if
  292. the  packet has been acknowledged.  Pkt.End-1 is the sequence number of the
  293. last byte in the packet.  Note that any packet on the retransmission  queue
  294. must  occupy  at  least  one  sequence number and therefore no special case
  295. checks must be made worry about Pkt.End-1 being less than Pkt.Seq .
  296.  
  297. TCP11  sends  each  newly   typed   character   in   a   separate   packet.
  298. Retransmissions carry all unacknowledged data.  TCP for the PDP-10/20 tries
  299. to  minimize internal storage requirements by saving a retransmitted packet
  300. and releasing the storage for the original transmissions.  Given a incoming
  301. packet InPkt, the following test is performed against  each  packet  queued
  302. for   action  by  the  buffer  reassembler:  Include(InPkt.Seq,  InPkt.End,
  303. QdPkt.Seq, QdPkt.End) means that the incoming packet contains at  least  as
  304. much as the already queued packet and that the latter may be released.
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.                                    - 5 -
  321.  
  322.  
  323.  
  324.  
  325. Internet Experiment Note 74                              21 September 1978
  326. Sequence Number Arithmetic                               William W. Plummer
  327.  
  328.  
  329. 7.      Sample Code
  330.  
  331. The  following  routines  have  been  excerpted from the hand coded TCP for
  332. TENEX and TOPS20.  They have been included here to provide an indication of
  333. complexity.  Note that the PDP-10 has a 36-bit word size  and  thus  32-bit
  334. numbers  are  always  positive.   Operations  such  as CAM which are signed
  335. compares on 36-bit quantites are unsigned operations on 32-bit  numbers  as
  336. required.
  337.  
  338.  
  339. ; CheckWindow(Left, Sequence, Right)
  340.  
  341. ; Test "Sequence" to see if it is between "Left" (inclusive) and "Right"
  342. ; (not incl.).  Sequence numbers are  modulo MAXSEQ and are always
  343. ; positive when represented in a 36-bit word.
  344.  
  345. ;T1/    Left
  346. ;T2/    Sequence
  347. ;T3/    Right
  348. ;
  349. ;       CALL CHKWND
  350. ;Ret+1: always.  T1 non-zero if Sequence is in the window
  351.  
  352. CHKWND::TEMP <VAL,SEQ,RIGHT,LEFT> ; Give names to T1, T2, T3, T4
  353.         MOVEM T1,LEFT           ; Make T1 available for value
  354.         SETO VAL,               ; Init value to TRUE
  355.         CAMG LEFT,RIGHT         ; Crosses 0?
  356.          TDZA VAL,VAL           ; No.  Set VAL to FALSE.
  357.          EXCH LEFT,RIGHT        ; Yes.  Reverse Left and Right.
  358.         CAMGE SEQ,RIGHT
  359.         CAMGE SEQ,LEFT
  360.         CAIA
  361.          SETCA VAL,             ; Complement VAL.
  362.         RESTORE
  363.         RET
  364.  
  365. By   way  of  comparison,  the  BCPL  expression  for  this  compiles  into
  366. approximately 40 instructions on the PDP-10.  This expression is:
  367.  
  368.     let CheckWindow(L, S, R) := (L le R) => (L le S < R), (R le S < L)
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.                                    - 6 -
  380.  
  381.  
  382.  
  383.  
  384. Internet Experiment Note 74                              21 September 1978
  385. Sequence Number Arithmetic                               William W. Plummer
  386.  
  387.  
  388. ; Test to see if two sequence number segments have one or more common
  389. ; points.  The two segments are semi-open on the right, similar
  390. ; to CHKWND.
  391.  
  392. ;T1/    Left1
  393. ;T2/    Right1
  394. ;T3/    Left2
  395. ;T4/    Right2
  396. ;
  397. ;       CALL OVRLAP
  398. ;Ret+1  always, T1 non-zero if overlap exists
  399.  
  400. OVRLAP::LOCAL <LEFT1,LEFT2,RIGHT2> ; Define some local ACs.
  401.         MOVEM T1,LEFT1
  402.         DMOVEM T3,LEFT2         ; T3,T4 to LEFT2,RIGHT2
  403.         EXCH T2,T3
  404.         CALL CHKWND
  405.         JUMPN T1,OVRLAX
  406.         MOVE T1,LEFT2
  407.         MOVE T2,LEFT1
  408.         MOVE T3,RIGHT2
  409.         CALL CHKWND
  410. OVRLAX: RESTORE
  411.         RET
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.                                    - 7 -
  439.  
  440.